/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.losalpes.catalogo.utilidades;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Writer;

/**
 *
 * @author AndhyMao
 */
public class ReadWriteTextFile {

    public static StringBuffer getContentFile(File file) {
        StringBuffer content = new StringBuffer();
        try {
            BufferedReader bufferedReader =  new BufferedReader(new FileReader(file));
            try {
                String line = null;
                while ((line = bufferedReader.readLine()) != null) {
                    content.append(line);
                    content.append(System.getProperty("line.separator"));
                }
            }
            finally {
                bufferedReader.close();
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return content;
    }

    public static StringBuffer setContentFile(File file, StringBuffer content) {
        try {
            Writer writer = new BufferedWriter(new FileWriter(file));
            try {
              writer.write(content.toString());
            }
            finally {
              writer.close();
            }
        }
        catch (IOException e){
            e.printStackTrace();
        }
        catch (Exception e){
            e.printStackTrace();
        }
        return content;
    }

    public static void delete(String directory, String nameFile) {
        try {
            File file = new File(directory + File.separator + nameFile);
            if (file.exists() && !file.isDirectory() && file.canWrite()) {
                file.delete();
            }
        }
        catch (Exception e){
            e.printStackTrace();
        }
    }
}
